package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.AdminSystem;
import com.example.entity.Teacher;
import com.example.service.TeacherService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 教职工登录
     * @return
     */
    @PostMapping("/loginTeacher")
    public R login(@RequestBody Teacher teacher){

        LambdaQueryWrapper<Teacher> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(teacher.getJobNumber()),Teacher::getJobNumber,teacher.getJobNumber());
        lqw.eq(Strings.isNotEmpty(teacher.getPassword()),Teacher::getPassword,teacher.getPassword());

        Teacher one = teacherService.getOne(lqw);
        if (one == null){
            return R.error("登录失败！");
        }else {
            return R.success("登录成功！",one);
        }
    }

    /**
     * 教职工分页查询
     * @return
     */
    @GetMapping("/getPageTeacher/{current}/{size}")
    public R getPageTeacher(@PathVariable int current,@PathVariable int size){

        IPage page =new Page(current,size);

        IPage page1 = teacherService.page(page);

        return R.success("教职工分页查询！",page1);
    }

    /**
     * 添加教职工
     * @return
     */
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody Teacher teacher){

        LambdaQueryWrapper<Teacher> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(teacher.getJobNumber()),Teacher::getJobNumber,teacher.getJobNumber());
        //1.查询数据库此工号是否存在
        Teacher one = teacherService.getOne(lqw);

        //2.不存在在添加
        if (one==null){
            boolean save = teacherService.save(teacher);
            if (save){
                return R.success("添加教职工成功！",null);
            }else {
                return R.error("添加教职工失败！");
            }
        }else {
            //3.存在
            return R.error("工号存在！添加失败！");
        }
    }

    /**
     * 通过id查询教职工
     * @return
     */
    @GetMapping("/getTeacherById")
    public R getTeacherById(@PathVariable int id){

        LambdaQueryWrapper<Teacher> lqw =new LambdaQueryWrapper<>();


        Teacher one = teacherService.getOne(lqw);

        return R.success("通过id查询教职工",one);
    }

    /**
     * 通过id修改教职工
     * @return
     */
    @PutMapping("/updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher){


        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.success("通过id修改教职工成功",teacher);
        }else {
            return R.error("通过id修改教职工失败！");
        }
    }

    /**
     * 通过id删除教职工
     * @return
     */
    @DeleteMapping("/deleteTeacher/{id}")
    public R deleteTeacher(@PathVariable int id){

        boolean b = teacherService.removeById(id);
        if (b){
            return R.success("通过id删除教职工成功",id);
        }else {
            return R.error("通过id删除教职工失败！");
        }
    }

    /**
     * 根据id查询教职工信息
     * @return
     */
    @GetMapping("/getMyself/{id}")
    public R getMyself(@PathVariable int id){

        Teacher one = teacherService.getById(id);
        return R.success("根据id查询教职工信息(修改回显)",one);
    }


}

